var winWidth;
var winHeight;
var hideBtn;
var showBtn;
var leftMenu;
var hideMenuBtns;
var hideMenuLanguageBtns;
var mainArea;
var mainBody;
var tableContent;
var mainNavBg;
var mainNavBtns;
var pageTitle;

var ifMenuHide = false;

$(function(){
    if(navigator.userAgent.indexOf('Windows')>-1){
        $('table thead tr').append('<th class="scrollFixTh" style="width: 18px;"></th>');
    }

    setTimeout(initMain,50);

    function initMain() {
        hideBtn = $(".leftMenu>.hideMenuBtn");
        showBtn = $(".leftMenu>.showLeftMenu");
        leftMenu = $(".leftMenu");
        hideMenuBtns = $(".showHide");
        hideMenuLanguageBtns = $(".leftMenu>.languageChangeImg>.changeImg");
        mainArea = $(".mainArea");
        mainBody = $(".mainArea>.mainBody");
        tableContent = $(".mainArea>.mainBody>.tableContent");
        mainNavBg = $(".mainArea>.nav>.curBg");
        mainNavBtns = $(".mainArea>.nav>.navBtn");
        pageTitle = $(".mainArea>.header>.pageTitle");

        hideBtn.bind("click", hideLeftMenu);
        showBtn.bind("click", showLeftMenu);
        hideMenuLanguageBtns.bind("click", clickLanguageBtns);
        mainNavBtns.bind("mouseover", overNav);
        mainNavBtns.bind("mouseout", outNav);
        $(".mainArea>.mainBody>.tableContent>table tr img.showImg").bind("mouseover",overImg);
        $(".mainArea>.mainBody>.tableContent>table tr img.showImg").bind("mouseout",outImg);

        hideMenuBtns.hide();
        hideMenuBtns.css("opacity",1);

        onResize();

        setTimeout(checkCurNav,50);

        $(window).resize(function(){
            setTimeout(onResize,50);
        });
    }

    function overImg(event){
        var curImg = $(event.currentTarget);
        var tTop = curImg.position().top-130;
        var tLeft = curImg.position().left+270;
        var tempImg = "<img height='300' src='"+curImg.attr("src")+"' style='position: absolute; top: "+tTop+"px; left: "+tLeft+"px;' class='tempImg'/>";
        $(".popUpImg").append(tempImg);
    }
    function outImg(){
        $(".popUpImg").html("");
    }

    function onResize(){
        winWidth = Math.max($(window).width(),1004);
        winHeight = Math.max($(window).height(),620);
        var mainBodyHeight = winHeight - 114;
        var tableContentHeight = mainBodyHeight - $('.btnArea').height() - 15;
        if($(".mainArea>.mainBody>.btnArea").hasClass("smallArea")){
            tableContentHeight = tableContentHeight + 46;
        }

        $(".mainArea>.shadow").css("height",winHeight+"px");
        showBtn.css("top",(winHeight-95)/2);
        mainArea.css("width",checkMainAreaWidth()+"px");
        mainBody.css("width",checkMainBodyWidth()+"px");
        mainBody.css("height",mainBodyHeight+"px");
        tableContent.css("height",tableContentHeight+"px");

        if($('.mainArea > .mainBody > .tableContent').height()<=$('.mainArea > .mainBody > .tableContent > table').height()){
            $('.scrollFixTh').removeClass('noScrollFixTh');
        }else{
            $('.scrollFixTh').addClass('noScrollFixTh');
        }
    }

    function checkMainAreaWidth(){
        var mainAreaWidth = ifMenuHide?43:213;
        mainAreaWidth = winWidth - mainAreaWidth;
        return mainAreaWidth;
    }

    function checkMainBodyWidth(){
        var mainBodyWidth = checkMainAreaWidth();
        mainBodyWidth = mainBodyWidth - 56;
        return mainBodyWidth;
    }

    function hideLeftMenu(){
        ifMenuHide = true;
        leftMenu.children(":not(.showHide)").fadeOut(300);
        leftMenu.delay(300).animate({width: 43}, 300, "easeOutQuad");
        mainArea.delay(300).animate({width: checkMainAreaWidth(),left:43}, 300, "easeOutQuad");
        mainBody.delay(300).animate({width: checkMainBodyWidth()}, 300, "easeOutQuad");
        pageTitle.delay(300).animate({margin:"12px 0 0 100px"},300,"easeOutQuad");
        hideMenuBtns.delay(600).fadeIn(300);
    }

    function showLeftMenu(){
        ifMenuHide = false;
        hideMenuBtns.fadeOut(300);
        leftMenu.delay(300).animate({width: 213}, 300, "easeOutQuad");
        mainArea.delay(300).animate({width: checkMainAreaWidth(),left:213}, 300, "easeOutQuad");
        mainBody.delay(300).animate({width: checkMainBodyWidth()}, 300, "easeOutQuad");
        pageTitle.delay(300).animate({margin:"12px 0 0 43px"},300,"easeOutQuad");
        leftMenu.children(":not(.showHide)").delay(600).fadeIn(300);
    }

    function clickLanguageBtns(event){
        var cur = $(event.currentTarget);
        if(cur.hasClass("cur")){
            hideMenuLanguageBtns.toggleClass("show");
        }else{
            location.href = cur.attr("link");
        }
    }

    function checkCurNav(){
        mainNavBtns.each(function(i,navBtn){
            if($(navBtn).hasClass("cur")){
                var bgLeft = $(navBtn).position().left;
                var bgWidth = $(navBtn).width()+30;
                mainNavBg.css("left",bgLeft+"px");
                mainNavBg.css("width",bgWidth+"px");
            }
        })
    }

    function overNav(event){
        var navBtn = event.currentTarget;
        var bgLeft = $(navBtn).position().left;
        var bgWidth = $(navBtn).width()+30;
        mainNavBg.stop().animate({left:bgLeft,width:bgWidth},300,"easeOutQuad");
    }

    function outNav(){
        mainNavBtns.each(function(i,navBtn){
            if($(navBtn).hasClass("cur")){
                var bgLeft = $(navBtn).position().left;
                var bgWidth = $(navBtn).width()+30;
                mainNavBg.stop().animate({left:bgLeft,width:bgWidth},300,"easeOutQuad");
            }
        })
    }
});

function beSureDelete(delText,delLink){
    if(delText==''){
        delText = 'Be sure to delete？';
    }
    var isSure = confirm(delText);
    if(isSure==true){
        location.href = delLink;
    }
}

function getByteLen(val) {
    var len = 0;
    for (var i = 0; i < val.length; i++) {
        if (val[i].match(/[^\x00-\xff]/ig) != null)
            len += 2;
        else
            len += 1;
    }
    return len;
}

function getByteVal(val, max) {
    var returnValue = '';
    var byteValLen = 0;
    for (var i = 0; i < val.length; i++) {
        if (val[i].match(/[^\x00-\xff]/ig) != null)
            byteValLen += 2;
        else
            byteValLen += 1;
        if (byteValLen > max)
            break;
        returnValue += val[i];
    }
    return returnValue;
}